import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.combine(4, 2));
    }
}

class Solution {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> ans = new ArrayList<>();
        List<Integer> cur = new ArrayList<>(k);
        backtracking(n, k, 1, ans, cur);
        return ans;
    }

    void backtracking(int n, int k, int i, List<List<Integer>> ans, List<Integer> cur) {
        if (cur.size() + n - i + 1 < k || cur.size() > k) {
            return;
        }
        if (cur.size() == k) {
            ans.add(new ArrayList<>(cur));
            return;
        }
        backtracking(n, k, i + 1, ans, cur);
        cur.add(i);
        backtracking(n, k, i + 1, ans, cur);
        cur.remove(cur.size() - 1);
    }
}
